/**
 * 用户登录
 * @author  Jello
 * @create  2018/10/31 17:24
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var token = "";
var curPage = 1;
var size = 6;
var workId = "";
var array = new Array();
var spinner = new Spinner(opts);

$(function() {
	//检测是否记住密码
	var loginSign = localStorage.getItem("phone_local");
	var pwdSign = localStorage.getItem("pwd_local");
	if(pwdSign) {
		$("#loginPhone").val(loginSign);
		$("#loginPwd").val($.base64.decode(pwdSign));
		$("#checkbox").attr("checked", true);
	} else {
		$("#loginPhone").val(loginSign);
		$("#loginPwd").val("");
		$("#checkbox").attr("checked", false);
	}

	//轮播图
	var mySwiper = new Swiper('.swiper-container', {
		initialSlide: 0,
		// 轮播图的方向，也可以是vertical方向
		direction: 'horizontal',
		//播放速度
		loop: true,
		observer: true,
		observeParents: true,
		// 自动播放时间
		autoplay: 2500,
		// 播放的速度
		speed: 300,
		// 如果需要分页器，即下面的小圆点
		pagination: '.swiper-pagination',
		// 这样，即使我们滑动之后， 定时器也不会被清除
		autoplayDisableOnInteraction: false,
		paginationClickable: true,
		paginationType: 'bullets',
	});

	//查询理想岗位及曾任职工作
	var param = {};
	param.token = token;
	queryMechanismList(param, function(mechanismList) {
		var obj = new Array();
		for(var i = 0; i < mechanismList.length; i++) {
			var html = '<div class="item" value="' + mechanismList[i].id + '"><div class="product"><span>' + mechanismList[i].mechanismTypeName + '</span><img src="img/common/white.png" /></div></div>';
			$(".all-goods").append(html);
			var workTypeList = mechanismList[i].workTypeList;
			for(var j = 0; j < workTypeList.length; j++) {
				array.push(workTypeList[j].mechanismTypeId + "-" + workTypeList[j].workTypeName);
			}
		}
	})

	$('.all-goods .item').hover(function() {
		$(this).addClass('active')
		var goodId = $(this).attr("value");
		var html = "";
		$.each(array, function(index, item) {
			if(goodId == item.split("-")[0]) {
				html += '<span onclick="hideLevelTab(' + item.split("-")[0] + ',\'' + item.split("-")[1] + '\')">' + item.split("-")[1] + '</span>';
			}
		});
		var _html = '<div class="product-wrap pos01"><div class="cf">' + html + '</div></div>';
		$(".item").append(_html);
		$(this).find('.product-wrap').show();
	}, function() {
		$(this).removeClass('active')
		$(this).find('.product-wrap').hide();
	});

	//登录、注册弹出层
	$("#loginbtn").click(function() {
		stopScroll();
		$(".loginBox").show();
	})
	$("#registbtn").click(function() {
		stopScroll();
		$(".registBox").show();
	})
	//登录、注册遮罩层
	$(".logincover").click(function() {
		$(document).unbind("scroll");
		$(".loginBox").hide();
	})
	$(".registcover").click(function() {
		$(document).unbind("scroll");
		$(".registBox").hide();
	})

	//登录监听
	$("#loginPhone").on('input propertychange', function() {
		var phone = $(this).val();
		checkPhone(phone, "error2", "error1");
	})

	$("#loginPhone").click(function() {
		$(this).css("background", 'url(img/icon/phone.png) no-repeat 12px center');
	})
	$("#loginPwd").click(function() {
		$(this).css("background", 'url(img/icon/pwd.png) no-repeat 12px center');
	})

	$("#loginPwd").on('input propertychange', function() {
		var pwd = $(this).val();
		checkPwd(pwd, "error2", "error1");
	})
	$("#loginPhone").blur(function() {
		$("#error2").html("");
		$("#error1").hide();
		$(this).css("background", '');
	})
	$("#loginPwd").blur(function() {
		$("#error2").html("");
		$("#error1").hide();
		$(this).css("background", '');
	})
	//注册监听
	$("#registPhone").on('input propertychange', function() {
		var phone = $(this).val();
		checkPhone(phone, "error4", "error3");
	})
	$("#registPwd").on('input propertychange', function() {
		var pwd = $(this).val();
		checkPwd(pwd, "error4", "error3");
	})
	$("#registPhone").click(function() {
		$(this).css("background", 'url(img/icon/phone.png) no-repeat 12px center');
	})
	$("#registPwd").click(function() {
		$(this).css("background", 'url(img/icon/pwd.png) no-repeat 12px center');
	})
	$("#registPhone").blur(function() {
		$("#error4").html("");
		$("#error3").hide();
		$(this).css("background", '');
	})
	$("#registPwd").blur(function() {
		$("#error4").html("");
		$("#error3").hide();
		$(this).css("background", '');
	})
	//验证码监听
	$("#code").click(function() {
		$(this).css("background", 'url(img/icon/code.png) no-repeat 12px center');
	})
	$("#code").blur(function() {
		$("#error4").html("");
		$("#error3").hide();
		$(this).css("background", '');
	})

	//查看更多
	$("#morehot_btn").click(function() {
		window.location.href = "favorjob.html"
	})

	//登录
	$("#loginbox_btn").click(function() {
		var phone = $("#loginPhone").val();
		var pwd = $("#loginPwd").val();
		if(checkPhone(phone, "error2", "error1") && checkPwd(pwd, "error2", "error1")) {
			//登录接口上送
			var param = {};
			param.phone = phone;
			param.pwd = pwd;
			login(param, function(resultObj) {
				if(resultObj.data.msg == "null") {
					$("#error2").html("用户不存在");
					$("#error1").show();
				} else if(resultObj.data.msg == "error") {
					$("#error2").html("用户名或密码错误");
					$("#error1").show();
				} else {
					rememberPwd(); //回调记住密码
					sessionStorage.setItem("user_token", resultObj.data.userToken); //存储token
					sessionStorage.setItem("user_sign", resultObj.data.id); //登录返回userid，去验证是否登录
					window.location.href = "index.html";
				}
			})
		}
	});

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#registPhone").val(); //手机号
		var pwd = $("#registPwd").val(); //密码

		var checkValue = (checkPhone(phoneNum, "error4", "error3") && checkPwd(pwd, "error4", "error3"));
		if(checkValue) {
			//获取验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 0;
			gainCode(param, function(resultObj) {
				if(resultObj.code == 0) {
					token = resultObj.data; //获取验证码成功后返回的token
					//开启验证码定时器
					for(i = 1; i <= 60; i++) {
						if(i == 1) {
							window.setTimeout("countDown(" + i + ")");
						} else {
							window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
						}
					}
				} else {
					$("#error4").html(resultObj.msg);
					$("#error3").show();
					return;
				}
			});
		}
	})

	//注册
	$("#registbox_btn").click(function() {
		var phoneNum = $("#registPhone").val(); //手机号
		var pwd = $("#registPwd").val(); //密码
		var code = $("#code").val(); //验证码
		var flag = (checkPhone(phoneNum, "error4", "error3") && checkPwd(pwd, "error4", "error3") && checkCode(code));
		if(flag) {
			var checked = $("#registcheck").prop("checked"); //协议checkbox
			if(!checked) {
				$("#error4").html("请阅读并勾选协议");
				$(".errorMsg2").show();
				return;
			}
			//校验验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 0;
			param.code = code;
			param.token = token;
			mateCode(param, function(mateObj) {
				if(mateObj.code == 0) {
					//注册接口上送
					var param = {};
					param.phone = phoneNum;
					param.pwd = pwd;
					regist(param, function() {
						$(".registBox").hide();
						$(".errorbox").show();
						$(".errorMsg2").hide();
						setTimeout(function() {
							$(".errorbox").hide();
							window.location.href = "regist.html";
						}, 800);
					})
				} else {
					$("#error4").html(mateObj.msg);
					$("#error3").show();
					return;
				}
			})
		}
	})

	//免费登录
	$("#free_login").click(function() {
		window.open("regist.html");
	})

	//免费注册
	$("#free_regist").click(function() {
		window.open("regist.html?type=1");
	})

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword", companyName);
		window.location.href = "applyjob.html";
	})

	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});
	//客服中心
	$("#service_btn1").click(function() {
		window.open("tencent://message/?uin=1423335266&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("tencent://message/?uin=2028048473&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("tencent://message/?uin=3341118684&Site=lanpin123.com&Menu=yes");
	})

	//查询热门招聘
	flipOverPage(curPage);

	$(".errcover").click(function() {
		$(document).unbind("scroll");
		$(".errorbox").hide();
	})
})

/**
 * 验证码倒计时
 * @param {Object} count 倒数
 */
function countDown(count) {
	if(count == 60) {
		$("#code_btn").html("获取验证码");
		$("#code_btn").attr('disabled', false);
	} else {
		left_time = 60 - count;
		$("#code_btn").attr('disabled', true);
		$("#code_btn").html(left_time + "秒重新发送");
	}
}

/**
 * 记住密码
 */
function rememberPwd() {
	var phoneNum = $("#loginPhone").val(); //手机号
	var pwd = $("#loginPwd").val(); //密码
	var checked = $('#checkbox').is(':checked');
	if(checked) { //选中记住密码,永久存储本地
		localStorage.setItem("phone_local", phoneNum);
		localStorage.setItem("pwd_local", $.base64.encode(pwd));
	} else {
		localStorage.removeItem("pwd_local");
	}
}

/**
 * 校验手机号
 */
function checkPhone(phone, showTxt, showId) {
	var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if(phone == "") {
		$("#" + showTxt).html("手机号码不能为空");
		$("#" + showId).show();
		return;
	} else if(!reg.test(phone)) {
		$("#" + showTxt).html("手机号码格式错误");
		$("#" + showId).show();
		return;
	} else {
		$("#" + showTxt).html("");
		$("#" + showId).hide();
	}
	return true;
}

/**
 * 校验密码
 * @param {Object} pwd
 */
function checkPwd(pwd, showTxt, showId) {
	if(pwd == "") {
		$("#" + showTxt).html("密码不能为空");
		$("#" + showId).show();
		return;
	} else if(pwd.indexOf(" ") >= 0) {
		$("#" + showTxt).html("密码中不能有空格字符");
		$("#" + showId).show();
		return;
	} else if((/[\u4e00-\u9fa5]+/).test(pwd)) {
		$("#" + showTxt).html("密码不能包含中文");
		$("#" + showId).show();
		return;
	} else if(pwd.length < 6 || pwd.length > 16) {
		$("#" + showTxt).html("密码长度为6-16位");
		$("#" + showId).show();
		return;
	} else {
		$("#" + showTxt).html("");
		$("#" + showId).hide();
	}
	return true;
}

/**
 * 检验验证码
 * @param {Object} code
 */
function checkCode(code) {
	var reg = /^[0-9]*$/;
	if(code == "") {
		$("#error4").html("验证码不能为空");
		$("#error3").show();
		return;
	} else if(!reg.test(code)) {
		$("#error4").html("验证码格式不正确");
		$("#error3").show();
		return;
	}
	return true;
}

/**
 * 查询热门招聘
 */
function flipOverPage(curPage) {
	//查询热门招聘
	$.ajax({
		type: "get",
		url: serverUrl + "/workapi/get_work_list_api?page=" + curPage + "&size=" + size + "&type=1" + "&sortType=1" + "&status=0",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: asktime,
		dataType: "json",
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			$(".indexJob").empty();
			if(resultObj.code == 0 && resultObj.data.list.length != 0) {
				for(var i = 0; i < resultObj.data.list.length; i++) {
					var lendType = ""; //借支
					var workType = "";
					var money = "";
					var keyList = "";
					var reStatus = "none";
					var workList = resultObj.data.list;

					if(workList[i].lendType == 1) {
						lendType = "可借支";
					}
					if(workList[i].postType == 1) {
						workType = "小时工";
						money = workList[i].workMoney + '元/小时';
					} else if(workList[i].postType == 0) {
						workType = "正式工";
						money = workList[i].wageRange + '元/月';
					}
					if(workList[i].status == 1) { //0 正在招工 1 已经招满
						recruitStatus1 = "block";
						recruitStatus2 = "none";
					} else {
						recruitStatus1 = "none";
						recruitStatus2 = "block";
					}
					if(workList[i].key == null) {
						keyList = "";
					} else {
						keyList = workList[i].key; //关键字
					}

					if(workList[i].reStatus == 1) {
						reStatus = "block";
					}

					var index_html = '<li><div class="content_work" onclick="queryWorkDetail(' + workList[i].id + ',' + workList[i].lendType + ')"><div class="content_work_top">' +
						'<span class="post" style="color: #FF5454;display: ' + recruitStatus1 + '">已招满</span>' +
						'<span class="post" style="display: ' + recruitStatus2 + '">需' + workList[i].workTypeName + '' + workList[i].maxNumber + '人</span>' +
						'<span class="salary">' + money + '</span></div>' +
						'<div class="content_work_center"><span class="worktype">' + workType + '</span>' +
						'<span class="lend">' + lendType + '</span><span class="applynum">已报名' + workList[i].applyNumber + '人</span></div>' +
						'<div class="content_work_bottom1"><img src="' + workList[i].mechanismUrl + '" /></div><div class="content_work_bottom2">' +
						'<span class="companyname">' + workList[i].mechanismName + '</span><span class="key">' + keyList + '</span>' +
						'<div class="title7" style="display:' + reStatus + '"><span>返' + workList[i].reMoney + '</span></div><span class="score">' + workList[i].mechanismScore + '分</span>' +
						'</div></div>' +
						'</li>';
					$(".indexJob").append(index_html);
				}
			} else { //没有热门招聘
				$(".indexJob").hide();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			closeSpin(300);
			return;
		}
	});
}

/**
 * 查询招聘详情
 */
function queryWorkDetail(id, lend) {
	workId = id;
	sessionStorage.setItem("lendType", lend);
	window.location.href = "jobdetail.html?id=" + workId;
}

/**
 * 根据行业和岗位查询求职招聘
 */
function hideLevelTab(mechanismType, mechanismName) {
	$(".industrycon2").hide();
	sessionStorage.setItem("industrykey", mechanismType);
	sessionStorage.setItem("postnamekey", mechanismName);
	window.location.href = "applyjob.html";
}

/**
 * 跳转求职
 */
function applyPage() {
	sessionStorage.removeItem('industrykey');
	sessionStorage.removeItem('postnamekey');
	sessionStorage.removeItem('keyword');
	window.location.href = "applyjob.html";
}

/**
 * 跳转热门招聘
 */
function favorPage() {
	window.location.href = "favorjob.html";
}